package main

import (
	"net/http"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

type LoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

// 硬编码的用户名和密码（实际项目中应使用数据库和加密存储）
const (
	validUsername = "admin"
	validPassword = "@Lin998877"
)

func main() {
	r := gin.Default()

	// 添加CORS中间件，允许跨域
	r.Use(cors.Default())

	r.POST("/login", handleLogin)

	// 启动服务器在端口8990
	r.Run(":8990")
}

func handleLogin(c *gin.Context) {
	var req LoginRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求格式"})
		return
	}

	// 验证用户名和密码
	if req.Username != validUsername || req.Password != validPassword {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码不正确"})
		return
	}

	// 成功登录
	c.JSON(http.StatusOK, gin.H{"success": true, "message": "登录成功"})
}